<?php
namespace app\index\service;
//订单
use ServS\Times;
use think\Db;
class OrderService{
	//添加订单
	public function addorder($parm){
		
		$order_sn = Times::getdate().time().rand(1000,9999).$parm["uid"];
		
		$dresscn = Db::table("ys_user_address")
		                       ->where("id",$parm['dress'])->find();
		$goods_spec_detail = Db::table("ys_goods_spec_detail")
		                       ->where("id",$parm['goods_spec_detail'])->find();
		$goods = Db::table("ys_goods")
		                       ->where("id",$parm['goods_id'])->find();
           
		$data = [ "uid"             =>$parm["uid"],
				  "coupon_id" 		=>"", 							//优惠券id
				  "coupon_price" 	=>'', 							//优惠券价格
				  "order_type" 		=>1,	 						//线上
				  "order_sn" 		=>$order_sn, 					//订单编号
				  "store_id" 		=>$parm["storeid"], 			//门店id
				  "tuihuo_sign"		=>0, 							//0 未退货 1全部退货 2部分退货
				  "order_status" 	=>0, 							//订单状态,0待支付，1待发货，2已确认，3商家已发货，4已收货，5申请退货，6退货完成，7订单完成，8订单已取消（超过30分钟未支付就置为取消状态）
				  "shipping_status" =>0,  							//发货状态
				  "consignee" 		=>$dresscn['consignee'],  		//收货人
				  "country" 		=>$dresscn['country'],	 		//国家
				  "province" 		=>$dresscn['province'], 		//省份
				  "city" 			=>$dresscn['city'], 			//城市
				  "district" 		=>$dresscn['city'], 			//县区
				  "twon" 			=>$dresscn['area'], 			//乡镇
				  "address"	 		=>$dresscn['area'], 			//地址
				  "zipcode" 		=>$dresscn['zipcode'], 			//邮政编码
				  "mobile" 			=>$dresscn['mobile'], 			//手机
				  "email" 			=>$dresscn['email'], 			//邮件
				  "shipping_code" 	=>'', 							//物流code
				  "shipping_name" 	=>'', 							//物流名称
				  "pay_code" 		=>'', 							//支付code,商户端订单号
				  "pay_type" 		=>'', 							//支付方式，1支付宝，2微信，3余额 ，4现金，5银行卡
				  "prepayid" 		=>'', 							//预支付码（微信扫码支付使用）
				  "pay_status" 		=>0, 							//支付状态,0,未支付，1已支付
				  "pay_name" 		=>'', 							//支付方式名称
				  "pay_time" 		=>'', 							//支付时间
				  "invoice_title" 	=>'', 							//发票抬头
				  "goods_num" 		=>$parm["sum"],					//商品数量
				  "goods_price" 	=>$parm["sum_price"], 			//商品总价
				  "shipping_price" 	=>'', 							//邮费
				  "order_amount"    =>'',  							//应付款金额
				  "receive_amount"  =>'', 							//线下订单专用，实收金额
				  "final_amount" 	=>$parm["sum_price"], 			//最终交易金额，（店家修改价格）包括扣除代金券
				  "dis_type" 		=>'',							//1加价，2减价
				  "discount" 		=>'',  							//价格调整,后台修改价格
				  "addtime" 		=>time(), 						//下单时间
				  "shipping_time" 	=>'', 							//最后新发货时间
				  "confirm_time" 	=>'', 							//收货确认时间
				  "transaction_id"  =>'',							//第三方平台交易流水号
				  "user_note" 		=>'',							//用户备注
				  "admin_note" 		=>'',							//管理员备注
				  "score" 			=>'', 							//积分,订单完成时才发放积分
				  "order_comment" 	=>'',							//会员对订单的评价
				  "img_comment" 	=>'',							//评价上传图片
				  "level_comment" 	=>'',							//评价星级1~5星
				  "time_comment" 	=>'',							//评价时间
				  "is_deleted" 		=>0,							//用户假删除标识,1:删除,0未删除

			 ];
		 //进入订单表
		 $orderid =  Db::table("ys_order")->insertGetId($data);
		 //进入订单详情表
		 $datadetaile =[
		 		   "uid"	    	=>$parm["uid"], 	//
		 		   "order_id"		=>$orderid,     					// 支付订单id，对应门店的订单id
		 		   "spec_id"		=>$parm["goods_spec_detail"],		 //属性id  
		 		   "spec_detail"	=>$goods_spec_detail["title"],		// 商品属性详情，（例如存上：   尺寸: 8寸 口味: 苹果 颜色: 红色）
		 		   "goods_id"		=>$parm["goods_id"],				//商品id
		 		   "goods_num"		=>$parm["sum"],						//购买数量
		 		   "spec_price"		=>$goods_spec_detail["price"],		//商品原始单价
		 		   "discount_signal_price"	=>'',						//商品单价,平摊优惠券之后的单价(退款时候使用）
		 		   "final_amount"	=>$parm["sum_price"],				//商品最终支付总额，扣除优惠券的价格
		 		   "bar_code"		=>$goods["good_number"], 			//条码
		 		   "is_comment"		=>'',  								//是否评价
		 		   "tuihuo_sign"	=>0, 								//退货标志，0未退货，1全部退货，2部分退货
		 		   "tuihuo_num"		=>'', 								//退货数量
		 		   "tuihuo_amount"	=>'', 								//退货金额
		 		   "tuihuo_time"	=>'', 								//退货申请时间
		 		   "th_deal_admin"	=>'', 								//管理员id，处理人id
		 		   "th_deal_remark"	=>'', 								//退货处理备注
		 		   "status"			=>0, 								//订单状态,0未支付，1已支付，2商家已确认，3商家已发货，4已收货，5申请退货，6退货完成，7订单完成，8订单已取消（超过30分钟未支付就置为取消状态）
		 		   "delivery_id"	=>'', 								//发货单ID
		 		   "storeid"		=>$parm["storeid"], 				//门店id
		 		   "is_online"		=>1,
		 		   "store_goods_stock_id" =>$parm["store_goods_stock"],	//门店商品id
		 		   "addtime"		=>time()
		 	];
		 $orderdetaileid =  Db::table("ys_order_details")->insertGetId($datadetaile);
		 if($orderid&&$orderdetaileid){
		 	 //订单装填 
		 	 		$ordercationdata =[
		 	 			"uid" =>$parm["uid"],        //下单人id
		 	 			"order_id" =>$orderid,       //订单id
		 	 			"action_user" =>0,           //操作人 0 为用户操作，其他为管理员id
		 	 			"order_status" =>0,          //订单状态
		 	 			"pay_status" =>0,            //支付状态
		 	 			"status_desc" =>"直接下订单",//状态描述
		 	 			"storeid" =>$parm["storeid"], //门店id
		 	 			"addtime" =>time()
		 		 ];
		 	  Db::table("ys_order_action")->insertGetId($ordercationdata);
		 	return $orderid;
		 }
		 return false;
	}

	//购物车进入订单
	public function addorderbycar($parm){
		$order_sn = Times::getdate().time().rand(1000,9999).$parm["uid"];
		$dresscn  = Db::table("ys_user_address")
		                       ->where("id",$parm['dress'])->find();
		//购车集合
		$ordercarids = $ordercaids  	   = explode(',', $parm["ordercarids"]);
		$sum = 0;
		$goods_sun_price = 0;
		foreach ($ordercarids as $key => $value) {
			$ordercar = Db::table("ys_order_cart")->where("id",$value)->find();
			$sum 				+=  (int)$ordercar["goods_sum"];
			$goods_sun_price 	+=  (int)$ordercar["goods_sum_price"];
		}

			$data = [ "uid"             =>$parm["uid"],
				  "coupon_id" 		=>"", 					//优惠券id
				  "coupon_price" 	=>'', 					//优惠券价格
				  "order_type" 		=>1,	 				//线上
				  "order_sn" 		=>$order_sn, 			//订单编号
				  "store_id" 		=>$ordercar["storeid"], //门店id
				  "tuihuo_sign"		=>0, 					//0 未退货 1全部退货 2部分退货
				  "order_status" 	=>0, 					//订单状态,0待支付，1待发货，2已确认，3商家已发货，4已收货，5申请退货，6退货完成，7订单完成，8订单已取消（超过30分钟未支付就置为取消状态）
				  "shipping_status" =>0,  					//发货状态
				  "consignee" 		=>$dresscn['consignee'],//收货人
				  "country" 		=>$dresscn['country'],	//国家
				  "province" 		=>$dresscn['province'], //省份
				  "city" 			=>$dresscn['city'], 	//城市
				  "district" 		=>$dresscn['city'], 	//县区
				  "twon" 			=>$dresscn['area'], 	//乡镇
				  "address"	 		=>$dresscn['area'], 	//地址
				  "zipcode" 		=>$dresscn['zipcode'], 	//邮政编码
				  "mobile" 			=>$dresscn['mobile'], 	//手机
				  "email" 			=>$dresscn['email'], 	//邮件
				  "shipping_code" 	=>'', 	 	//物流code
				  "shipping_name" 	=>'', 	 	//物流名称
				  "pay_code" 		=>'', 	 	//支付code,商户端订单号
				  "pay_type" 		=>'', 	 	//支付方式，1支付宝，2微信，3余额 ，4现金，5银行卡
				  "prepayid" 		=>'', 	 	//预支付码（微信扫码支付使用）
				  "pay_status" 		=>0, 	 	//支付状态,0,未支付，1已支付
				  "pay_name" 		=>'', 	 	//	支付方式名称
				  "pay_time" 		=>'', 	 	//支付时间
				  "invoice_title" 	=>'', 	 	//发票抬头
				  "goods_num" 		=>$sum ,  	//商品数量
				  "goods_price" 	=>$goods_sun_price, 	//商品总价
				  "shipping_price" 	=>'', 		//邮费
				  "order_amount"    =>'',  	 	//应付款金额
				  "receive_amount"  =>'', 	 	//线下订单专用，实收金额
				  "final_amount" 	=>$sum, 	//最终交易金额，（店家修改价格）包括扣除代金券
				  "dis_type" 		=>'',	 	//1加价，2减价
				  "discount" 		=>'',   	//价格调整,后台修改价格
				  "addtime" 		=>time(),   //下单时间
				  "shipping_time" 	=>'', 		//最后新发货时间
				  "confirm_time" 	=>'', 	 	//收货确认时间
				  "transaction_id"  =>'',	 	//第三方平台交易流水号
				  "user_note" 		=>'',	 	//用户备注
				  "admin_note" 		=>'',	 	//管理员备注
				  "score" 			=>'', 	 	//积分,订单完成时才发放积分
				  "order_comment" 	=>'',	 	//会员对订单的评价
				  "img_comment" 	=>'',	 	//评价上传图片
				  "level_comment" 	=>'',	 	//评价星级1~5星
				  "time_comment" 	=>'',	 	//评价时间
				  "is_deleted" 		=>0,	 	//用户假删除标识,1:删除,0未删除

			 ];
			//进入订单表
		$orderid =  Db::table("ys_order")->insertGetId($data);   
		$ordercarsaveall = [];
		foreach ($ordercarids as $key => $value) {
			$ordercar = Db::table("ys_order_cart")->where("id",$value)->find();
			$goods    =Db::table("ys_goods")->where("id",$ordercar["goods_id"])->find();
			$goods_spec_detail = Db::table("ys_goods_spec_detail")->where("id",$ordercar["specid"])->find();
			$datadetaile =[
		 		   "uid"	    	=>$parm["uid"], 				//
		 		   "order_id"		=>$orderid,     				// 支付订单id，对应门店的订单id
		 		   "spec_id"		=>$ordercar["specid"], 			//属性id  
		 		   "spec_detail"	=>$goods_spec_detail["title"],	// 商品属性详情，（例如存上：   尺寸: 8寸 口味: 苹果 颜色: 红色）
		 		   "goods_id"		=>$ordercar["goods_id"],		//商品id
		 		   "goods_num"		=>$ordercar["goods_sum"],		//购买数量
		 		   "spec_price"		=>$ordercar["goods_price"],		//商品原始单价
		 		   "discount_signal_price"	=>'',					//商品单价,平摊优惠券之后的单价(退款时候使用）
		 		   "final_amount"	=>$ordercar["goods_sum_price"], //商品最终支付总额，扣除优惠券的价格
		 		   "bar_code"		=>$goods["good_number"], 		//条码
		 		   "is_comment"		=>'',  							//是否评价
		 		   "tuihuo_sign"	=>0, 							//退货标志，0未退货，1全部退货，2部分退货
		 		   "tuihuo_num"		=>'', 							//退货数量
		 		   "tuihuo_amount"	=>'', 							//退货金额
		 		   "tuihuo_time"	=>'', 							//退货申请时间
		 		   "th_deal_admin"	=>'', 							//管理员id，处理人id
		 		   "th_deal_remark"	=>'', 							//退货处理备注
		 		   "status"			=>0, 							//订单状态,0未支付，1已支付，2商家已确认，3商家已发货，4已收货，5申请退货，6退货完成，7订单完成，8订单已取消（超过30分钟未支付就置为取消状态）
		 		   "delivery_id"	=>'', 							//发货单ID
		 		   "storeid"		=>$ordercar["storeid"], 		//门店id
		 		   "is_online"		=>1,
		 		   "store_goods_stock_id" =>$ordercar["store_goods_stock_id"],	//门店商品id
		 		   "addtime"		=>time()
		 	];
		 	array_push($ordercarsaveall, $datadetaile);

		}
		$orderdetaileid =  Db::table("ys_order_details")->insertAll($ordercarsaveall);        
		if($orderdetaileid&&$orderid){
			 $ordercationdata =[
		 	 			"uid" =>$parm["uid"],        //下单人id
		 	 			"order_id" =>$orderid,       //订单id
		 	 			"action_user" =>0,           //操作人 0 为用户操作，其他为管理员id
		 	 			"order_status" =>0,          //订单状态
		 	 			"pay_status" =>0,            //支付状态
		 	 			"status_desc" =>"购物车下单",//状态描述
		 	 			"storeid" =>$ordercar["storeid"], //门店id
		 	 			"addtime" =>time()
		 	 ];
		 	  Db::table("ys_order_action")->insertGetId($ordercationdata);
			foreach ($ordercarids as $key => $value) {
				//$ordercar = Db::table("ys_order_cart")->where("id",$value)->delete();
			}
			return $orderid;
		}
		return false;
	}
}